
class WjlProxy extends ResourceProxyBase {
    public static NAME: string = "WjlProxy";//必须和excel导出文件一致
    public static Proxy: WjlProxy = null;
    public constructor() {
        super(WjlProxy.NAME);
    }
    public static getProxy() {
        if (!WjlProxy.Proxy) {
            WjlProxy.Proxy = new WjlProxy();
        }
        return WjlProxy.Proxy;
    }

    //登录成功
    public login(data: Object) {
        WjlVO.VO = null;
        WjlVO.getVo().lUserScore = GameVO.getVo().lUserScore;
    }
    //获取房间配置信息，获取完成后发送匹配玩家请求
    public getRoomInfo(data: Object) {
        WjlVO.getVo().dwServerRule = data["dwServerRule"];
        WjlVO.getVo().wChairCount = data["wChairCount"];
        WjlVO.getVo().wServerType = data["wServerType"];
        WjlVO.getVo().wTableCount = data["wTableCount"];
    }
    //用户进入广场 携带用户所有数据 3-100
    public userJoinSquare(data: Object) {
        let user = data["tagMobileUserInfoHead"];
        WjlVO.getVo().userInfo[user.dwUserID] = user;;//广场所有人存入集合
        this.userJoinRoom();
    }
    //用户战绩信息刷新 包括金币 3-101
    public getUserInfo(data: Object) {
        let UserScore = data["UserScore"];
        let dwUserID = data["dwUserID"];
        let lScore = UserScore["lScore"];
        let user = WjlVO.getVo().userInfo[dwUserID];
        if (user) {
            user.lScore = lScore;
        }
        this.userJoinRoom();
    }
    //用户状态变更 入座，离开 3-102
    public userStateModify(data: Object) {
        //用户状态发生改变从缓存的所有用户中找到该用户改变其座位
        let dwUserID = data["dwUserID"];
        let wTableID = data["tableId"];
        let wChairID = data["wChairID"];
        let cbUserStatus = data["cbUserStatus"];
        let user = WjlVO.getVo().userInfo[dwUserID]
        if (user) {
            user.wTableID = wTableID;
            user.wChairID = wChairID;
            user.cbUserStatus = cbUserStatus;
        }
        this.userJoinRoom();
    }

    public userJoinRoom() {
        WjlVO.getVo().initUserInof();
        //状态改变完毕后从所有玩家中找自己是否已入座，若自己入座，则找是否跟自己同桌的用户，将其显示到桌子对面
        let user = WjlVO.getVo().userInfo[GameVO.getVo().dwUserID]
        if (user) {
            WjlVO.getVo().wTableId = user.wTableID;
            WjlVO.getVo().wChairId = user.wChairID;
            WjlVO.getVo().status = user.cbUserStatus
            WjlVO.getVo().lUserScore = user.lScore
        }
        //我入座找对手
        if (WjlVO.getVo().wTableId != null && WjlVO.getVo().wTableId != 65535) {
            WjlVO.getVo().wTableId_up = WjlVO.getVo().wTableId;
            //根据我所在椅子确定其他玩家在桌面坐哪个位置
            for (let i in WjlVO.getVo().userInfo) {
                let user = WjlVO.getVo().userInfo[i]
                if (user.wTableID == WjlVO.getVo().wTableId &&
                    user.dwUserID != GameVO.getVo().dwUserID) {//跟我坐一桌的就是对手
                    WjlVO.getVo().playerNumber++;
                }
            }
            game.AppFacade.instance.sendNotification(PanelNotify.WJL.JOIN_ROOM);
        } else {
            if (WjlVO.getVo().wTableId_up != null && WjlVO.getVo().wTableId == 65535) {
                //上次有座位此次没座位说明被踢了
                SocketManager.closeSocket(InstructionsEnum.GAME_TYPE_NAME.WJL);
            }
        }
    }

    //游戏开始
    public gameStart(data: Object) {
        WjlVO.getVo().initGameInfo()
        WjlVO.getVo().game_state = 1;
        WjlVO.getVo().gameNum++;
        WjlVO.getVo().cbTimeLeave = data["cbTimeLeave"];       //剩余时间
        WjlVO.getVo().wBankerUser = data["wBankerUser"];       //庄家用户
        WjlVO.getVo().lBankerScore = data["lBankerScore"];        //庄家金币
        game.AppFacade.instance.sendNotification(PanelNotify.WJL.START_GAME);
    }
    //下注返回
    public betReceive(data: Object) {
        let wChairID = data["wChairID"];       //用户位置
        let cbBetArea = data["cbBetArea"];        //筹码区域
        let lBetScore = data["lBetScore"];        //加注数目
        WjlVO.getVo().betUser = wChairID;
        WjlVO.getVo().betAdds = cbBetArea;
        WjlVO.getVo().betNum = lBetScore;
        switch (cbBetArea) {
            case 6://闲对子
                WjlVO.getVo().betNumAdds_0 += lBetScore
                break;
            case 1://和
                WjlVO.getVo().betNumAdds_1 += lBetScore
                break;
            case 7://庄对子
                WjlVO.getVo().betNumAdds_2 += lBetScore
                break;
            case 0://闲
                WjlVO.getVo().betNumAdds_3 += lBetScore
                break;
            case 8://辛运6
                WjlVO.getVo().betNumAdds_4 += lBetScore
                break;
            case 2://庄
                WjlVO.getVo().betNumAdds_5 += lBetScore
                break;
        }
        if (wChairID == WjlVO.getVo().wChairId) {
            WjlVO.getVo().game_state = 2
            WjlVO.getVo().gameNum = 0;
            WjlVO.getVo().lUserScore -= lBetScore;
            WjlVO.getVo().betNumAdds_my += lBetScore;
            let user = WjlVO.getVo().userInfo[GameVO.getVo().dwUserID];
            if (user) {
                user.lScore = WjlVO.getVo().lUserScore;
            }
            switch (cbBetArea) {
                case 6://闲对子
                    WjlVO.getVo().betNumAdds_0_my += lBetScore
                    break;
                case 1://和
                    WjlVO.getVo().betNumAdds_1_my += lBetScore
                    break;
                case 7://庄对子
                    WjlVO.getVo().betNumAdds_2_my += lBetScore
                    break;
                case 0://闲
                    WjlVO.getVo().betNumAdds_3_my += lBetScore
                    break;
                case 8://辛运6
                    WjlVO.getVo().betNumAdds_4_my += lBetScore
                    break;
                case 2://庄
                    WjlVO.getVo().betNumAdds_5_my += lBetScore
                    break;
            }
        }
        game.AppFacade.instance.sendNotification(PanelNotify.WJL.BET_RECEIVE);
    }
    //游戏结束
    public gameOver(data: Object) {
        WjlVO.getVo().cbTimeLeave = data["cbTimeLeave"]
        let cbCardCount: Array<any> = data["cbCardCount"];//两家各自牌数量
        let cbTableCardArray: Array<Array<any>> = data["cbTableCardArray"];//两家的牌，上面数量是几，这里就从数组中取几个
        let cbWinArea: Array<any> = data["cbWinArea"];//所有区域得分
        let lPlayAllScore = data["lPlayAllScore"];//玩家输赢
        WjlVO.getVo().poker_xian = cbTableCardArray[0].slice(0, cbCardCount[0])
        WjlVO.getVo().poker_zhuang = cbTableCardArray[1].slice(0, cbCardCount[1])
        WjlVO.getVo().score_arr = cbWinArea;
        WjlVO.getVo().score_my = lPlayAllScore
        //我的当前金币等我现在金币加上总下注加上输赢分
        WjlVO.getVo().lUserScore += WjlVO.getVo().betNumAdds_my += lPlayAllScore
        let user = WjlVO.getVo().userInfo[GameVO.getVo().dwUserID];
        if (user) {
            user.lScore = WjlVO.getVo().lUserScore;
        }
        game.AppFacade.instance.sendNotification(PanelNotify.WJL.GAME_OVER);
    }
    //100-101 中途加入
    public dataRecovery(data: Object) {
        if (RegDataWjl.MainCMD_MsgId[100][101] == RegDataWjl.gameStart) {
            WjlVO.getVo().initGameInfo()
            WjlVO.getVo().cbTimeLeave = data["cbTimeLeave"] //剩余时间
            WjlVO.getVo().cbGameStatus = data["cbGameStatus"] //游戏状态
            var lAllBet: Array<any> = data["lAllBet"]//所有区域已下注
            var lPlayBet: Array<any> = data["lPlayBet"] //玩家下注
            WjlVO.getVo().lAreaLimitScore = data["lAreaLimitScore"]//区域下注限制
            WjlVO.getVo().betNumAdds_0 = lAllBet[6]
            WjlVO.getVo().betNumAdds_1 = lAllBet[1]
            WjlVO.getVo().betNumAdds_2 = lAllBet[7]
            WjlVO.getVo().betNumAdds_3 = lAllBet[0]
            WjlVO.getVo().betNumAdds_4 = lAllBet[8]
            WjlVO.getVo().betNumAdds_5 = lAllBet[2]
            WjlVO.getVo().betNumAdds_0_my = lPlayBet[6]
            WjlVO.getVo().betNumAdds_1_my = lPlayBet[1]
            WjlVO.getVo().betNumAdds_2_my = lPlayBet[7]
            WjlVO.getVo().betNumAdds_3_my = lPlayBet[0]
            WjlVO.getVo().betNumAdds_4_my = lPlayBet[8]
            WjlVO.getVo().betNumAdds_5_my = lPlayBet[2]
            WjlVO.getVo().betNumAdds_my = 0;
            for (let i in lPlayBet) {
                WjlVO.getVo().betNumAdds_my += lPlayBet[i];
            }
            game.AppFacade.instance.sendNotification(PanelNotify.WJL.DATA_RECOVERY);
        }
        if (RegDataWjl.MainCMD_MsgId[100][101] == RegDataWjl.free) {
            WjlVO.getVo().lAreaLimitScore = data["lAreaLimitScore"]//区域下注限制
            WjlVO.getVo().cbTimeLeave = data["cbTimeLeave"]
            game.AppFacade.instance.sendNotification(PanelNotify.WJL.WAIT_NEXT_GAME);
        }
    }
    //200-99空闲等待
    public waitNextGame(data: Object) {
        WjlVO.getVo().cbTimeLeave = data["cbTimeLeave"];       //剩余时间
        game.AppFacade.instance.sendNotification(PanelNotify.WJL.WAIT_START_GAME);
    }
    //清空路单
    public clearLudan() {
        WjlVO.getVo().ludan = [];
        game.AppFacade.instance.sendNotification(PanelNotify.WJL.RECEIVE_LUDAN);
    }
    //推送路单
    public receiveLuDan(data: Object) {
        var struGameRecord: Array<any> = data["struGameRecord"];
        for (var i = struGameRecord.length - 1; i >= 0; i--) {
            WjlVO.getVo().ludan.push(struGameRecord[i])
        }
        game.AppFacade.instance.sendNotification(PanelNotify.WJL.RECEIVE_LUDAN);
    }
    //推送场外人员
    public receiveUserList(data: Object) {
        var structUserList = data["structUserList"]
        WjlVO.getVo().structUserList = structUserList;
        var obj: Object = {
            lTakeMinScore: WjlVO.getVo().lUserScore,
            szCityName: GameVO.getVo().szNickName,
            wFaceID: GameVO.getVo().wFaceID
        }
        WjlVO.getVo().structUserList.push(obj)
        game.AppFacade.instance.sendNotification(PanelNotify.WJL.RECEIVE_USER_LIST);
    }
    //获取牌局编号
    public getSerialNumber(data: Object) {
        WjlVO.getVo().gameNumbering = data["szSerialNumber"];
        game.AppFacade.instance.sendNotification(PanelNotify.WJL.GET_SERIAL_NUMBER)
    }
}
